<script>
export default {
  name: 'CategoryEdit',
  data () {
    return {
      formData: {
        categoryId: '',
        name: '',
        profile: '',
        status: 1
      },
      rules: {
        name: [
          { required: true, message: '分类名不能为空' }
        ],
        status: [
          { required: true, message: '状态不能为空' }
        ]
      },
      show: false
    }
  },
  methods: {
    init (id) {
      this.formData = { ...this.$options.data().formData }
      if (id) {
        this.$axios.get(`category/info/${id}`).then(data => {
          this.formData = { ...data }
        }).catch(e => e)
      }
      this.show = true
    },
    submit () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$axios.post('category/update', this.formData).then(() => {
            this.$message.success('操作成功！')
            this.show = false
            this.$emit('close')
          }).catch(e => e)
        }
      })
    }
  }
}
</script>

<template>
<el-dialog v-if="show" title="编辑分类" :visible.sync="show" width="40%">
  <el-form :model="formData" :rules="rules" ref="formRef" label-width="120px">
    <el-form-item label="分类名" prop="name">
      <el-input v-model="formData.name" placeholder="请输入分类名称" clearable/>
    </el-form-item>
    <el-form-item label="简介">
      <el-input v-model="formData.profile" placeholder="请输入分类简介" type="textarea" :rows="4" :maxlength="120" show-word-limit/>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-radio-group v-model="formData.status">
        <el-radio :label="1">可用</el-radio>
        <el-radio :label="2">禁用</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</el-dialog>
</template>

<style scoped lang="scss">

</style>
